package com.frxuexi.park2304

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.mvi.BaseViewActivity
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.park2304.databinding.ActivitySplashBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import kotlin.concurrent.timerTask

class SplashActivity : BaseViewActivity<ActivitySplashBinding>() {
    override val binding: ActivitySplashBinding by lazy { ActivitySplashBinding.inflate(layoutInflater) }
    var timer:Timer?=null
    var num=5
    override fun initView() {
        val long = SPUtils.getInstance().getLong("time",0L)
        if(long!=0L && (System.currentTimeMillis()-long)/1000/60/60<24){
            start()
        }

        lifecycleScope.launch(Dispatchers.IO) {
            timer= Timer()
            timer!!.schedule(timerTask {
                lifecycleScope.launch(Dispatchers.Main) {
                    binding.splashTime.setText("${num}s")
                    if(num==0){
                        timer?.cancel()

                        SPUtils.getInstance().put("time",System.currentTimeMillis())
                        start()
                    }
                    num--
                }
            },0,1000)
        }

        binding.splashSkip.setOnClickListener {
            timer?.cancel()
            SPUtils.getInstance().put("time",System.currentTimeMillis())
            start()
        }
    }

    fun start(){
//        ARouter.getInstance().build("/app/LoginActivity").navigation()
        ArouterManager.nav(this,"/app/LoginActivity")
        finish()
    }


    override fun onDestroy() {
        super.onDestroy()
        if(timer!=null){
            timer?.cancel()
            timer=null
        }
    }

}